package com.example.demo.leetcode.top100;

import java.util.*;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _33排序链表
 * @description
 * @date 2023/06/12 20:58
 * <p>
 * ******************************************************
 */
public class _33排序链表 {

    public ListNode sortList(ListNode head) {
        if (head == null) return null;
        List<ListNode> list = new ArrayList<>();
        while (head != null) {
            list.add(head);
            head = head.next;
        }
        Comparator<ListNode> comparator = (o1, o2) -> o1.val - o2.val;
        list.sort(comparator);

        for (int i = 0; i < list.size()-1; i++) {
            list.get(i).next = list.get(i+1);
        }
        list.get(list.size()-1).next = null;
        return list.get(0);
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}
